package com.baosight.sczx.zm.cf.service;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.zm.cf.domain.ZMCFNotice;

/**
 * 发送通知
 * <p>Title: ServiceZMCF15.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-7-8 上午05:03:21  严洪浩  Create
 *</pre>
 */
public class ServiceZMCF15 extends ServiceEPBase {
	
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:14:44 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:14:50 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		if(inInfo.getBlock(EiConstant.queryBlock) == null){
			StaticTools.setQueryParameters(inInfo, "fdEndDate", StaticTools.getDate() + " 23:59:59", true);
		}
		else {
			String endTime = inInfo.getBlock(EiConstant.queryBlock).getCellStr(0, "fdEndDate");
			if(null != endTime && !"".equals(endTime)) {
				if(endTime.length() <= 10) {
					StaticTools.setQueryParameters(inInfo, "fdEndDate", endTime + " 23:59:59", true);
				}
			}
		}
		
		String fdPersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//编制
		StaticTools.setQueryParameters(inInfo, "fdPersonId", fdPersonId, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCFNotice.query", new ZMCFNotice());
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:01 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdPersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//编制人ID
		String fdPerson = User.getInstance().getNameById(fdPersonId);
		String fdTime = StaticTools.getDateTime();//编制时间
		String fdIsApproval = inInfo.getBlock("result").getCellStr(0, "fdIsApproval");//是否审批
		String fdStatus = "草拟";
		String fdApprovalId = fdIsApproval.equals("1") ? inInfo.getBlock("result").getCellStr(0, "fdApprovalId") : null;
		String fdApproval = null;
		if(fdApprovalId != null) {
			fdApproval = User.getInstance().getNameById(fdApprovalId);
		}
		
		String fdKeyId = TablePk.getInstance().getNextValue("T_CF_NOTICE");
		inInfo.getBlock("result").setCell(0, "fdKeyId", fdKeyId);//主键
		inInfo.getBlock("result").setCell(0, "fdPersonId", fdPersonId);
		inInfo.getBlock("result").setCell(0, "fdPerson", fdPerson);
		inInfo.getBlock("result").setCell(0, "fdTime", fdTime);
		inInfo.getBlock("result").setCell(0, "fdStatus", fdStatus);
		inInfo.getBlock("result").setCell(0, "fdApprovalId", fdApprovalId);
		inInfo.getBlock("result").setCell(0, "fdApproval", fdApproval);
		inInfo.getBlock("result").setCell(0, "fdType", "0");
		
		String title = "通知公告：" + inInfo.getBlock("result").getCell(0, "fdTitle").toString();
		Documents.insertDoc(inInfo, fdKeyId, title, "通知公告", "其它", "");//添加附件
		
		EiInfo outinfo = super.insert(inInfo, "ZMCFNotice.insert", "result");
		
		return outinfo;
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:10 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		String fdTime = StaticTools.getDateTime();//编制时间
		String fdIsApproval = inInfo.getBlock("result").getCellStr(0, "fdIsApproval");//是否审批
		String fdApprovalId = fdIsApproval.equals("1") ? inInfo.getBlock("result").getCellStr(0, "fdApprovalId") : null;
		String fdApproval = null;
		if(fdApprovalId != null) {
			fdApproval = User.getInstance().getNameById(fdApprovalId);
		}

		inInfo.getBlock("result").setCell(0, "fdTime", fdTime);
		inInfo.getBlock("result").setCell(0, "fdApprovalId", fdApprovalId);
		inInfo.getBlock("result").setCell(0, "fdApproval", fdApproval);
		inInfo.getBlock("result").setCell(0, "fdType", "0");
		inInfo.getBlock("result").setCell(0, "fdStatus", "草拟");
		
		String title = "通知公告：" + inInfo.getBlock("result").getCell(0, "fdTitle").toString();
		Documents.insertDoc(inInfo, inInfo.getBlock("result").getCellStr(0, "fdKeyId"), title, "通知公告", "其他", "");//添加附件
		
		EiInfo outInfo = super.update(inInfo, "ZMCFNotice.update");//主表更新

		return outInfo;
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		//删除附件信息
		for(int i=0; i<inInfo.getBlock("result").getRowCount(); i++)
		{
			Documents.deleteDoc(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		
		EiInfo _temp = super.delete(inInfo, "ZMCFNotice.delete", "result");
		
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 生效
	 * <p>Function: release</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-31上午07:24:30 严洪浩
	 * </p>
	 *
	 */
	public EiInfo release(EiInfo inInfo) {
		EiBlock _block = inInfo.getBlock("result");
		for(int i = 0; i < _block.getRowCount(); i ++) {
			String fdIsApproval = inInfo.getBlock("result").getCellStr(i, "fdIsApproval");//是否审批
			String fdStatus = fdIsApproval.equals("1") ? "待审批" : "已发布";
			inInfo.getBlock("result").setCell(i, "fdStatus", fdStatus);
		}
		EiInfo _temp = super.update(inInfo, "ZMCFNotice.release");
		EiInfo outInfo = this.query(inInfo);
       
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 置顶
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	public EiInfo top(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCFNotice.modifyTop");
		
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 取消置顶
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	public EiInfo cancelTop(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCFNotice.modifyCancelTop");
		
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 查询附件
	 * <p>Function: queryFiles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30上午10:38:30 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryFiles(EiInfo inInfo) {
		String keyId = inInfo.getString("fdKeyId");
		return Documents.getFilesInfo("filesresult", keyId);
	}
}
